package com.ruoyi.standingbook.maintenance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.maintenance.domain.bo.MaintenanceImportDetailBo;
import com.ruoyi.standingbook.maintenance.domain.vo.MaintenanceImportDetailVo;
import com.ruoyi.standingbook.maintenance.domain.MaintenanceImportDetail;
import com.ruoyi.standingbook.maintenance.mapper.MaintenanceImportDetailMapper;
import com.ruoyi.standingbook.maintenance.service.IMaintenanceImportDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 其它管养导入明细Service业务层处理
 *
 * @author cc
 * @date 2023-08-29
 */
@RequiredArgsConstructor
@Service
public class MaintenanceImportDetailServiceImpl implements IMaintenanceImportDetailService {

    private final MaintenanceImportDetailMapper baseMapper;

    /**
     * 查询其它管养导入明细
     */
    @Override
    public MaintenanceImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询其它管养导入明细列表
     */
    @Override
    public TableDataInfo<MaintenanceImportDetailVo> queryPageList(MaintenanceImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MaintenanceImportDetail> lqw = buildQueryWrapper(bo);
        Page<MaintenanceImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询其它管养导入明细列表
     */
    @Override
    public List<MaintenanceImportDetailVo> queryList(MaintenanceImportDetailBo bo) {
        LambdaQueryWrapper<MaintenanceImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MaintenanceImportDetail> buildQueryWrapper(MaintenanceImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MaintenanceImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), MaintenanceImportDetail::getItemName, bo.getItemName());
        lqw.eq(bo.getServiceType()!=null, MaintenanceImportDetail::getServiceType, bo.getServiceType());
        lqw.like(StringUtils.isNotBlank(bo.getMaintenanceName()), MaintenanceImportDetail::getMaintenanceName, bo.getMaintenanceName());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), MaintenanceImportDetail::getCommunity, bo.getCommunity());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), MaintenanceImportDetail::getLocation, bo.getLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getLocationDescribe()), MaintenanceImportDetail::getLocationDescribe, bo.getLocationDescribe());
        lqw.eq(bo.getDate() != null, MaintenanceImportDetail::getDate, bo.getDate());
        lqw.eq(StringUtils.isNotBlank(bo.getFrequency()), MaintenanceImportDetail::getFrequency, bo.getFrequency());
        lqw.eq(bo.getActualFrequency() != null, MaintenanceImportDetail::getActualFrequency, bo.getActualFrequency());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), MaintenanceImportDetail::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPic1()), MaintenanceImportDetail::getPic1, bo.getPic1());
        lqw.eq(StringUtils.isNotBlank(bo.getPic2()), MaintenanceImportDetail::getPic2, bo.getPic2());
        lqw.eq(StringUtils.isNotBlank(bo.getPic3()), MaintenanceImportDetail::getPic3, bo.getPic3());
        lqw.eq(StringUtils.isNotBlank(bo.getPic4()), MaintenanceImportDetail::getPic4, bo.getPic4());
        lqw.eq(StringUtils.isNotBlank(bo.getProviderId()), MaintenanceImportDetail::getProviderId, bo.getProviderId());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), MaintenanceImportDetail::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getProcessStatus() != null, MaintenanceImportDetail::getProcessStatus, bo.getProcessStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessMsg()), MaintenanceImportDetail::getProcessMsg, bo.getProcessMsg());
        return lqw;
    }

    /**
     * 新增其它管养导入明细
     */
    @Override
    public Boolean insertByBo(MaintenanceImportDetailBo bo) {
        MaintenanceImportDetail add = BeanUtil.toBean(bo, MaintenanceImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改其它管养导入明细
     */
    @Override
    public Boolean updateByBo(MaintenanceImportDetailBo bo) {
        MaintenanceImportDetail update = BeanUtil.toBean(bo, MaintenanceImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MaintenanceImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除其它管养导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean batchInsertVo(List<MaintenanceImportDetailVo> voList) {
        List<MaintenanceImportDetail> list= BeanCopyUtils.copyList(voList , MaintenanceImportDetail.class);
        return baseMapper.insertBatch(list);
    }
}
